package org.xncj.Controller;


import org.constant.MessageConstant;
import org.entity.PageResult;
import org.entity.QueryPageBean;
import org.entity.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.xncj.CheckGroupService;
import org.xncj.CheckGroup;
import org.xncj.dao.CheckItemDao;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("/CheckGroup")
public class CheckGroupController {
    @Resource
    CheckGroupService checkGroupService;
    @Autowired
    private CheckItemDao checkItemDao;
    private static final Logger logger = LoggerFactory.getLogger(CheckGroupController.class);

    @PostMapping("/AddCheckGroup/{ids}")
    public Result AddCheckGroup(@PathVariable("ids") Integer[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.AddCheckGroup(checkitemIds, checkGroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/findgroup")
    public PageResult findgroup(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findgroup(queryPageBean);
        return pageResult;
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteByid(@PathVariable("id") Integer checkGroupid) {
        try {
            checkGroupService.deleteByid(checkGroupid);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);

        }
    }
    @GetMapping("/findByCheckGroup/{id}")
    public Result findByCheckGroup(@PathVariable("id") Integer checkGroupid) {
        Integer[] checkItemIds = checkGroupService.findByCheckGroup(checkGroupid);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemIds);
    }

    @PutMapping("/updateCheckGroup/{Ids}")
    public Result updateCheckGroup(@PathVariable("Ids") Integer[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        try {
            logger.debug("Received updateCheckGroup request with checkitemIds: {}", Arrays.toString(checkitemIds));
            logger.debug("Received updateCheckGroup request with checkGroup: {}", checkGroup);

            checkGroupService.updateCheckGroup(checkitemIds, checkGroup);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            logger.error("Error occurred while processing updateCheckGroup request", e);
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
}
